<?php
	
	App::import('core', 'AppModel');
	App::import('behavior', 'LilSearch.LilSearch');
	ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.CAKE_CORE_INCLUDE_PATH.DS.'vendors');
	
	class TestBase extends AppModel {
		var $name = 'TestBase';
		var $cacheSources = false;
		var $hasMany = array('TestChild' => array('foreignKey'=>'base_id'));
	}
	
	class TestChild extends AppModel {
		var $name = 'TestChild';
		var $cacheSources = false;
		var $belongsTo = array('TestBase' => array('foreignKey' => 'base_id'));
	}
	
	
	class LilSearchCase extends CakeTestCase {
		var $Base = null;
		var $fixtures = array('plugin.lil_search.test_base', 'plugin.lil_search.test_child');
	
		function startCase() {
		    App::import('core', 'Folder');
			$f = new Folder();
			$f->delete(TMP.'lucene'.DS.'LilSearchTest');
			
		    $this->Base =& ClassRegistry::init('TestBase');
		    $this->Base->Behaviors->attach('LilSearch', array(
				'indexPath'    => '{TMP}lucene{DS}LilSearchTest',
			));
		}
		
		function endCase() {
		    $this->Base->Behaviors->detach('LilSearch');
		    unset($this->Base);
			ClassRegistry::flush();
			
		    App::import('core', 'Folder');
			$f = new Folder();
			$f->delete(TMP.'lucene'.DS.'LilSearchTest');
		}
	
		/* FIRST PART - WITHOUT FIELDS */
		function testTestBaseInstance() {
			$this->assertTrue(is_a($this->Base, 'TestBase'));
		}
		
		function testTestBaseFind() {
			$result = $this->Base->recursive = -1;
			$result = $this->Base->find('first');
			$this->assertTrue(!empty($result));
			$expected = array(
				'TestBase' => 
					array (
					'id' => '1',
					'name' => 'This is first title',
					'description' => 'This is a text description. I will be talking about Lil CakePHP plugins.',
					'created' => '2008-01-23 12:34:56',
					'modified' => '2008-03-21 12:34:56',
					),
				);
			$this->assertEqual($result, $expected);
		}
		
		function testTestBaseIndex() {
			$result = $this->Base->recursive = 1;
			$result = $this->Base->lilSearchIndex(1);
			$result = $this->Base->lilSearchIndex(2);
		}
		
		function testTestBaseLilSearchCount() {
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 2);
		}
		
		function testTestBaseLilSearchGetDocId() {
			$result = $this->Base->lilSearchGetDocId(2);
			$this->assertEqual($result, 1);
			
			$result = $this->Base->lilSearchGetDocId(1);
			$this->assertEqual($result, 0);
		}
		
		function testTestBaseLilSearch() {
			$expected = array('1');
			$result = $this->Base->lilSearch('CakePHP');
			$this->assertEqual($result, $expected);
			
			$expected = array('2');
			$result = $this->Base->lilSearch('"second title"');
			$this->assertEqual($result, $expected);
			
			$expected = array('1', '2');
			$result = $this->Base->lilSearch('this');
			$this->assertEqual($result, $expected);
			
			$expected = array();
			$result = $this->Base->lilSearch('notexistant');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('2008/08/20');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('prašiček');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('PRAŠIČEK');
			$this->assertEqual($result, $expected);
		}
		
		function testTestBaseLilSearchDelete() {
			$result = $this->Base->lilSearchDelete(2);
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 1);
		}
		
		/* SECOND PART - store fields */
		
		function testTestBaseInit2() {
		    $this->Base->Behaviors->detach('LilSearch');
		    App::import('core', 'Folder');
			$f = new Folder();
			$f->delete(TMP.'lucene'.DS.'LilSearchTest');
			
		    $this->Base->Behaviors->attach('LilSearch', array(
				'indexPath'	  => '{TMP}lucene{DS}LilSearchTest',
				'storeFields' => true
			));
			//$GLOBALS['LilSearchTest']['storeFields'] = true;
		}
		
		function testTestBaseInstance2() {
			$this->assertTrue(is_a($this->Base, 'TestBase'));
		}
		
		function testTestBaseFind2() {
			$result = $this->Base->recursive = -1;
			$result = $this->Base->find('first');
			$this->assertTrue(!empty($result));
			$expected = array(
				'TestBase' => 
					array (
					'id' => '1',
					'name' => 'This is first title',
					'description' => 'This is a text description. I will be talking about Lil CakePHP plugins.',
					'created' => '2008-01-23 12:34:56',
					'modified' => '2008-03-21 12:34:56',
					),
				);
			$this->assertEqual($result, $expected);
		}
		
		function testTestBaseIndex2() {
			$result = $this->Base->recursive = 1;
			$result = $this->Base->lilSearchIndex(1);
			$result = $this->Base->lilSearchIndex(2);
		}
		
		function testTestBaseLilSearchCount2() {
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 2);
		}
		
		function testTestBaseLilSearchGetDocId2() {
			$result = $this->Base->lilSearchGetDocId(2);
			$this->assertEqual($result, 1);
			
			$result = $this->Base->lilSearchGetDocId(1);
			$this->assertEqual($result, 0);
		}
		
		function testTestBaseLilSearch2() {
			$expected1 = array (
				'TestBase' => 
					array (
					'id' => '1',
					'name' => 'This is first title',
					'description' => 'This is a text description. I will be talking about Lil CakePHP plugins.',
					'created' => '2008-01-23 12:34:56',
					'modified' => '2008-03-21 12:34:56',
					),
				'TestChild' => 
					array (
						0 => 
							array (
								'id' => '1',
								'base_id' => '1',
								'title' => 'LilSearch Behavior',
								'description' => 'This behavior implements Zend Lucene Search. It supports Utf-8 words like prašiček and searching numbers like 2008/08/20',
								'created' => '2008-01-23 12:34:56',
								'modified' => '2008-03-21 12:34:56',
							),
						1 => 
							array (
								'id' => '2',
								'base_id' => '1',
								'title' => 'LilBayes Behavior, Component and Vendor',
								'description' => 'This is a Bayes Categorization component.',
								'created' => '2008-04-05 11:22:33',
								'modified' => '2008-11-22 11:22:33',
							),
					),
			);
			$expected2 = array (
				'TestBase' => 
					array (
					'id' => '2',
					'name' => 'This is second title',
					'description' => '',
					'created' => '2008-04-05 11:22:33',
					'modified' => null,
					),
			);	
			$result = $this->Base->lilSearch('CakePHP');
			$this->assertEqual($result, array(0=>$expected1));
			
			$expected = array('2');
			$result = $this->Base->lilSearch('"second title"');
			$this->assertEqual($result, array(0=>$expected2));
			
			$result = $this->Base->lilSearch('this');
			$this->assertEqual($result, array(0=>$expected1, 1=>$expected2));
			
			$expected = array();
			$result = $this->Base->lilSearch('notexistant');
			$this->assertEqual($result, $expected);
			
			$expected = array('1');
			$result = $this->Base->lilSearch('2008/08/20');
			$this->assertEqual($result, array(0=>$expected1));
			
			$expected = array('1');
			$result = $this->Base->lilSearch('prašiček');
			$this->assertEqual($result, array(0=>$expected1));
			
			$expected = array('1');
			$result = $this->Base->lilSearch('PRAŠIČEK');
			$this->assertEqual($result, array(0=>$expected1));
			
			$result = $this->Base->lilSearch(array('conditions'=>'this', 'order'=>'TestBase.created'));
			$this->assertEqual($result, array(0=>$expected1, 1=>$expected2));
			
			$result = $this->Base->lilSearch(array('conditions'=>'this', 'order'=>'TestBase.created DESC'));
			$this->assertEqual($result, array(0=>$expected2, 1=>$expected1));
			
			$result = $this->Base->lilSearch(array('conditions'=>'this', 'order'=>array('TestBase.created DESC', 'TestBase.description')));
			$this->assertEqual($result, array(0=>$expected2, 1=>$expected1));
		}
		
		function testTestBaseLilSearchDelete2() {
			$result = $this->Base->lilSearchDelete(2);
			$result = $this->Base->lilSearchCount();
			$this->assertEqual($result, 1);
		}
	}
?>
